﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Inventory_WarehouseHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridViewHistory_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ("Open" == e.CommandName)
        {
            int index = PublicDefine.GetSelectRowIndex(e);
            string Type = GridViewHistory.DataKeys[index].Values[0].ToString();
            string ListID = GridViewHistory.DataKeys[index].Values[1].ToString();
            if ("0" == Type)
            {
                Response.Redirect("DeliveredSheet.aspx?ListID=" + ListID);
            }
            else
            {
                Response.Redirect("WarehouseList.aspx?ListID=" + ListID);
            }
        }
        else
            if ("DeleteList" == e.CommandName)
        {
            int index = PublicDefine.GetSelectRowIndex(e);
            string Type = GridViewHistory.DataKeys[index].Values[0].ToString();
            string ListID = GridViewHistory.DataKeys[index].Values[1].ToString();
            if ("0" == Type)
            {
                DeleteDeliveredSheet(int.Parse(ListID));
            }
            else
            {
                DeleteWarehouseList(int.Parse(ListID));
            }
            GridViewHistory.DataBind();
        }

    }

    void DeleteWarehouseList(int listid)
    {
        using (SqlConnection connect = new SqlConnection(PublicDefine.SQLConnectString))
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = connect;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter;
            cmd.CommandText = "sp_WarehouseList_Delete";
            cmd.Parameters.Clear();
            parameter = new SqlParameter("@WarehouseListID", listid);
            cmd.Parameters.Add(parameter);
            cmd.ExecuteNonQuery();
            connect.Close();
        }
    }

    void DeleteDeliveredSheet(int listid)
    {
        using (SqlConnection connect = new SqlConnection(PublicDefine.SQLConnectString))
        {
            connect.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = connect;
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter parameter;
            cmd.CommandText = "sp_DeliveredSheet_Delete";
            cmd.Parameters.Clear();
            parameter = new SqlParameter("@DeliveredSheetID", listid);
            cmd.Parameters.Add(parameter);
            cmd.ExecuteNonQuery();
            connect.Close();
        }
    }

}
